using System;
using MSScriptControl;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.ManagedCode;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	/// <summary>
	/// Summary description for ScriptCodeOnLoadWorker.
	/// </summary>
	class ScriptCodeOnLoadWorker
	{
		public static void OnLoad(Form form)
		{
			ScriptSource scriptSource = form.Manifest.ScriptSource;
			if( scriptSource == null )
			{
				return;
			}

			try
			{	
				if( form.ScriptControlContext == null )
				{
					ScriptControlContext.buildContext( scriptSource, form );
				}

				XDocument xdoc = form.ScriptControlContext.XDocument;
				Application app = form.ScriptControlContext.Application;

				DocReturnEvent docReturnEvent = new DocReturnEvent( xdoc );

				ScriptControl scriptControl = form.ScriptControlContext.ScriptControl;

				foreach( Procedure proc in scriptControl.Procedures )
				{
					if( proc.Name.Equals( "XDocument_OnLoad" ) )
					{
						object[] param = new object[]{ docReturnEvent };
						scriptControl.Run( "XDocument_OnLoad", ref param );
					}
				}
			}
			catch( Exception e )
			{
                FormUtil.addInnerExceptionMassage(form, "XDocument_OnLoad", e);
			}

		}

	}

}
